package com.hschinese.pronunciation.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hschinese.pronunciation.R;
import com.hschinese.pronunciation.interfaces.LessonProgressInterface;
import com.hschinese.pronunciation.interfaces.NextLessonInterface;

import butterknife.Bind;
import butterknife.ButterKnife;
import studychinese.view.BaseStrokesHandWritingView;
import studychinese.view.BaseWordHandWritingView;

/**
 * Created by buba on 2018/2/5.
 */

public class BaseWordView extends LinearLayout implements View.OnClickListener{
    @Bind(R.id.bsv_view)
    BaseWordHandWritingView bsvView;
    @Bind(R.id.iv_hand_click)
    ImageView ivHandClick;
    @Bind(R.id.rl_view)
    RelativeLayout rlView;
    @Bind(R.id.tv_1)
    TextView tv1;
    @Bind(R.id.tv_2)
    TextView tv2;
    @Bind(R.id.tv_3)
    TextView tv3;
    @Bind(R.id.tv_4)
    TextView tv4;
    @Bind(R.id.iv_name)
    ImageView ivName; @Bind(R.id.rl_contstrction_parient)
    RelativeLayout rl_contstrction_parient;
    private int[] imgRes = {R.drawable.bishun_py_xzhy,R.drawable.bishun_py_xshx,R.drawable.bishun_py_xhhs,R.drawable.bishun_py_xzhb,R.drawable.bishun_py_xzhb,R.drawable.bishun_py_xwhn,R.drawable.bishun_py_xwhn,R.drawable.bishun_py_xwhn};
    private LessonProgressInterface mListener = null;
    private String name;
    private NextLessonInterface mNextListener = null;
    private int currentPosition;
    private Animation.AnimationListener wordViewAnimListener = null;
    private Animation chooseViewAnimation = null;
    // 每个空间都添加一种动画
    long durationMillis = 1000, delayMillis = 0;
    public BaseWordView(Context context) {
        super(context);
        initViews(context);
    }

    public BaseWordView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initViews(context);
    }

    private void initViews(Context context) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.base_word_view, null);
        ButterKnife.bind(this,inflate);
        ivHandClick.setOnClickListener(this);
        this.addView(inflate);
    }
    public void setData(int position){
        currentPosition = position;
        ivName.setImageResource(imgRes[position]);
        name = "base_bishun_"+position;
            ivHandClick.setVisibility(VISIBLE);
            bsvView.setVisibility(VISIBLE);
            setFirstAnimation();
        try {
            //显示小手，此时文字是填充的状态，点击小手后文字变为虚的字，然后自动填充
            ivHandClick.setVisibility(VISIBLE);
            bsvView.setText(name,false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (mListener != null) {
            mListener.setProgress(imgRes.length, position);
        }
        if (currentPosition!=0){
            addAnimation();
        }
    }
    private void setFirstAnimation() {
        //闪烁
        AlphaAnimation alphaAnimation1 = new AlphaAnimation(0.1f, 1.0f);
        alphaAnimation1.reset();
        alphaAnimation1.setDuration(1000);
        alphaAnimation1.setRepeatCount(2);
//        alphaAnimation1.setRepeatMode(Animation.);
        ivHandClick.setAnimation(alphaAnimation1);
        alphaAnimation1.start();
        //摇摆
       /* TranslateAnimation  alphaAnimation2 = new TranslateAnimation(150f, 180f, 50, 80);
        alphaAnimation2.setDuration(1000);
        alphaAnimation2.setRepeatCount(Animation.INFINITE);
        ivHandClick.setAnimation(alphaAnimation2);
        alphaAnimation2.start();*/
    }
    public void setListener(LessonProgressInterface listener) {
        mListener = listener;
    }
    public void setNextListener(NextLessonInterface listener) {
        mNextListener = listener;
    }
    private void addAnimation() {
        if (rl_contstrction_parient != null) {
            wordViewAnimListener = new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                }

                @Override
                public void onAnimationEnd(Animation animation) {


                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }
            };
            chooseViewAnimation = scaleIn(rl_contstrction_parient, durationMillis, delayMillis, wordViewAnimListener);
            rl_contstrction_parient.post(new Runnable() {
                @Override
                public void run() {
                    chooseViewAnimation.start();
                }
            });

        }
    }
    private Animation scaleIn(View view, long durationMillis, long delayMillis, Animation.AnimationListener l) {
        ScaleAnimation animation = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(durationMillis);
        animation.setStartOffset(delayMillis);
        view.setVisibility(View.VISIBLE);
        view.startAnimation(animation);
        animation.setAnimationListener(l);
        animation.cancel();
        return animation;
    }
    private Animation scaleBigIn(View view, long durationMillis, long delayMillis, Animation.AnimationListener l) {
        ScaleAnimation animation = new ScaleAnimation(0, 2, 0, 2, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(durationMillis);
        animation.setStartOffset(delayMillis);
        view.setVisibility(View.VISIBLE);
        view.startAnimation(animation);
        animation.setAnimationListener(l);
        animation.cancel();
        return animation;
    }
    public void beforeWord() {
        if (currentPosition <= 0)
            return;
        currentPosition--;
        if (currentPosition < imgRes.length) {
            setData(currentPosition);
        } /*else {
            // TODO 进入下一课. 会掉到StudyActivity页面来进行View的切换
            // mNextListener.onLessonChanged(lessonType - 1);
            weithLess--;
            currentPosition=0;
            initData();
        }*/
    }

   /* private void updateViewData() {
        if (currentPosition < 0) return;
        if (mListener != null) {
            mListener.setProgress(imgRes.length, currentPosition);
        }
    }*/
   public void nextWord() {
       // TODO 数据切换，涉及到界面上文字和图片的切换

       currentPosition++;
       if (currentPosition < imgRes.length) {
           setData(currentPosition);
       } /*else {
           weithLess++;
           currentPosition=0;
           initData();
           //  Utils.showDialog(mContext, R.string.next_lesson_dialog_msg, lessonType, mNextListener);
       }*/
   }
    @Override
    public void onClick(View view) {
        try {
            bsvView.setText(name,true);
            ivHandClick.setVisibility(GONE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
